$(function(){
	var obj = $(".func");
	obj.each(function(index){
		$(this).hover(function(){
			$(this).addClass("over");
			$(this).find(".modify").show();
		},function(){
			$(this).removeClass("over");
			$(this).find(".modify").hide();
		})
		$(this).click(function(){
			var id = this.id;
			var bindex = id.substring(0,id.indexOf('_'));
			var cid = $(this).attr("cid");
			$('#cid').val(cid);
			$("#brandIndex").val(bindex);
			$("#modifyWindow").window("open");
			$("#weightValue").focus();
		})
	});
	
	$("#modifyWindow").dialog({
		title:'修改',
		width:300,
		height:120,
		resizable:false,
		minimizable:false,
		maximizable:false,
		toolbar: [{
			text : '保存信息',
		    iconCls:'icon-save',
		    handler:function(){
		    	var indexValue = $('#weightValue').val();
		    	var selfIndex = $("#brandIndex").val();
		    	if(indexValue == ""){
		    		alert("请输入序号");
		    		return;
		    	}
		    	if(indexValue == selfIndex){
		    		alert("输入有误");
		    		return;
		    	}
		    	if(isNaN(indexValue)){
		    		alert("请输入数字");
		    		return;
		    	}else{
		    		indexValue = parseInt(indexValue,10);
		    		if(indexValue > 30 || indexValue < 1){
		    			alert("序号范围为1-30");
		    			return;
		    		}
		    	}
		    	var cid = $("#cid").val();
		    	var selfObj = $("#"+selfIndex+"_func_"+cid);
		    	var selfBrandId = selfObj.attr("bid");
		    	var selfWeight = $("#weight_"+selfBrandId).val();
		    	var targetObj = $("#"+indexValue+"_func_"+cid);
		    	var targetBrandId = targetObj.attr("bid");
		    	var targetWeight = $("#weight_"+targetBrandId).val();
		    	$.get('../brand/changePinggouIndex.shtml?sid='+selfBrandId+'&sweight='+selfWeight+'&tid='+targetBrandId+'&tweight='+targetWeight+'&sindex='+selfIndex+'&tindex='+indexValue,function(mes){
					if(mes == "1"){
						alert("修改成功");
						if(selfWeight == 0){
							selfWeight = 31 - selfIndex;
						}
						if(targetWeight == 0){
							targetWeight = 31 - indexValue;
						}
						$("#weight_"+selfBrandId).val(targetWeight);
						$("#weight_"+targetBrandId).val(selfWeight);
						$("#"+selfBrandId+"_index").html(indexValue);
						$("#"+targetBrandId+"_index").html(selfIndex);
						selfObj.attr("bid",targetBrandId);
						targetObj.attr("bid",selfBrandId);
						var htmlTemp = selfObj.html();
						selfObj.html(targetObj.html());
						targetObj.html(htmlTemp);
						$("#modifyWindow").dialog("close");
					}else{
						alert('修改失败');
						$("#modifyWindow").dialog("close");
					}		    		
		    	})
		    }
		  },{
			text : '替换该品牌',
		    iconCls:'icon-reload',
		    handler:function(){
		    	$.messager.confirm('确认操作','您是否要替换该广告品牌?',function(value){
		    		if(value==true){
		    			var cid = $("#cid").val();
		    			var selfIndex = $("#brandIndex").val();
		    			var selfObj = $("#"+selfIndex+"_func_"+cid);
		    			var selfBrandId = selfObj.attr("bid");
		    			var selfWeight = $("#weight_"+selfBrandId).val();
		    			$("#bweight").val(selfWeight);
		    			$("#bid").val(selfBrandId);
		    			$('#cid').val(cid);
			    		$("#modifyWindow").dialog("close");
			    		$("#brandWindow").window("open");
		    		}
		    	});
		    }
		  }],
		closed:true
	});
	$("#brandWindow").dialog({
		title:'品牌抽取',
		width:350,
		height:220,
		resizable:false,
		minimizable:false,
		maximizable:false,
		closed:true
	});
	
	$("#addWindow").dialog({
		title:'添加拼购品牌',
		width:350,
		height:220,
		resizable:false,
		minimizable:false,
		maximizable:false,
		closed:true
	});
	
});

function addByCategoryId(cid){
	$('#cid').val(cid);
	$("#addWindow").dialog('open');
}

function seachByKeyWord(){
	var cid = $("#cid").val();
	var key = $('#brandName').val();
	if(key == ""){
		alert("请输入检索的关键字");
		return;
	}
	$.get('../brand/searchPingou4IndexByKey.shtml?cid='+cid+'&page=1&rows=10000&name='+encodeURI(key),function(mes){
		var resultJson = mes['rows'];
		var str = "<table width='100%'>";
		var bidTemp = ',';
		for(var i = 0; i < resultJson.length; i++){
			if(bidTemp.indexOf(","+resultJson[i]['id']+",")!=-1)
			    continue;
			bidTemp += resultJson[i]['id']+',';
			str += "<tr><td>"+resultJson[i]['name']+"</td><td><img width='110px' height='34px' src='../"+resultJson[i]['imgsrc']+"'>" +
					"</td><td>"+resultJson[i]['companyName']+"</td><td><input onclick='changeBrand(\""+resultJson[i]['id']+"\",\""+resultJson[i]['name']+"\",\""+resultJson[i]['imgsrc']+"\",\""+resultJson[i]['sysuserId']+"\")' type='button' value='提取'/></td></tr>";
		}
		str += "</table>";
		$("#resultBrands").html(str);
	})
}

function seachByKeyWord2(){
	var cid = $("#cid").val();
	var key = $('#brandName2').val();
	if(key == ""){
		alert("请输入检索的关键字");
		return;
	}
	$.get('../brand/searchPingou4IndexByKey.shtml?cid='+cid+'&page=1&rows=10000&name='+encodeURI(key),function(mes){
		var resultJson = mes['rows'];
		var str = "<table width='100%'>";
		var bidTemp = ',';
		for(var i = 0; i < resultJson.length; i++){
			if(bidTemp.indexOf(","+resultJson[i]['id']+",")!=-1)
			    continue;
			bidTemp += resultJson[i]['id']+',';
			str += "<tr><td>"+resultJson[i]['name']+"</td><td><img width='110px' height='34px' src='../"+resultJson[i]['imgsrc']+"'>" +
					"</td><td>"+resultJson[i]['companyName']+"</td><td><input onclick='changeBrand2(\""+resultJson[i]['id']+"\",\""+resultJson[i]['name']+"\",\""+resultJson[i]['imgsrc']+"\",\""+resultJson[i]['sysuserId']+"\")' type='button' value='提取'/></td></tr>";
		}
		str += "</table>";
		$("#resultBrands2").html(str);
	})
}

function changeBrand(id,name,imgurl,pingouId){
	var bid = $("#bid").val();
	var bweight = $("#bweight").val();
	var selfIndex = $("#brandIndex").val();
	var cid = $("#cid").val();
	var selfObj = $("#"+selfIndex+"_func_"+cid);
	$.get('../brand/changePingouIndex.shtml?sid='+bid+'&tid='+id+'&cid='+cid+'&pid='+pingouId,function(mes){
		if("1" == mes){
			alert("替换成功");
			var str = '<img src="<%=basePath%>resource/css/easyui/icons/edit_remove.png" class="modify"/>'+
				'<img width="160px" height="40px" src="../'+imgurl+'"/><br/>'+
				'<span style="border-bottom:1px solid #ECECEC;">'+name+'</span><br/>'+
				'<span>序号：<b id="'+id+'_index">'+selfIndex+'</b><input type="hidden" id="weight_'+id+'" value="'+bweight+'"/></span>';
				selfObj.html(str);
				selfObj.attr("bid",id);
				$("#brandWindow").window("close");
		}
	})
	
}


function changeBrand2(id,name,imgurl,pingouId){
	var cid = $("#cid").val();
	var cindexObj = $("#"+cid+"_aindex");
	var cindex = parseInt(cindexObj.val(),10)+1;
	var selfObj = $("#container_"+cid);
	$.get('../brand/addPingouAd.shtml?bid='+id+'&pingouId='+pingouId+'&index='+cindex+'&cid='+cid,function(mes){
		if("1" == mes){
			alert("追加成功");
			var str = '<div class="func" align="center" id="'+(cindex)+'_func_'+cid+'" bid="'+id+'">' +
					'<img src="../resource/css/easyui/icons/edit_remove.png" class="modify"/>'+
				'<img width="160px" height="40px" src="../'+imgurl+'"/><br/>'+
				'<span style="border-bottom:1px solid #ECECEC;">'+name+'</span><br/>'+
				'<span>序号：<b id="'+id+'_index">'+cindex+'</b><input type="hidden" id="weight_'+id+'" value="'+(11-cindex)+'"/></span></div>';
				selfObj.html(selfObj.html()+str);
				cindexObj.val(cindex);
				if(cindex == 10){
					$('#'+cid+'_btn').hide();				
				}
				var obj = $(".func");
				obj.each(function(index){
					$(this).hover(function(){
						$(this).addClass("over");
						$(this).find(".modify").show();
					},function(){
						$(this).removeClass("over");
						$(this).find(".modify").hide();
					})
					$(this).click(function(){
						var id = this.id;
						var bindex = id.substring(0,id.indexOf('_'));
						$("#brandIndex").val(bindex);
						$("#modifyWindow").window("open");
						$("#weightValue").focus();
					})
				});
				$("#addWindow").window("close");
		}
	})
	
}